package com.ayf.payment.game.api.controller.platform;

import com.ayf.payment.game.api.dto.platform.MerchantSettleRateConfigDTO;
import com.ayf.payment.game.api.dto.platform.MerchantSettleRateDTO;
import com.ayf.payment.game.api.dto.platform.MerchantSettleRateDTO2;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.service.platform.MerchantSettleRateService;
import com.ayf.payment.game.api.type.CommonConstants;
import com.ayf.payment.game.api.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 费率
 *
 * @author wwl
 * @date 2020/3/10 19:26
 */
@RequestMapping("/t/settleRate")
@RestController
public class MerchantSettleRateController {
    @Autowired
    private MerchantSettleRateService merchantSettleRateService;

    /**
     * 查询列表
     * @return
     */
    @PostMapping("/selectAll")
    public CommonResult<List<MerchantSettleRateDTO2>> selectAll() {
        List<MerchantSettleRateDTO2> list=merchantSettleRateService.selectAll2();
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "成功", list);
    }

    /**
     * 查询详情
     * @param merchantSettleRateDTO
     * @return
     */
    @PostMapping("/selectOne")
    public CommonResult<MerchantSettleRateDTO> selectOne(@RequestBody MerchantSettleRateDTO merchantSettleRateDTO) {
        if(merchantSettleRateDTO==null){
            throw new ParameterException("参数不能为空");
        }
        if(merchantSettleRateDTO.getId()==null){
            throw new ParameterException("ID不能为空");
        }
        List<MerchantSettleRateDTO> list=merchantSettleRateService.selectAll(merchantSettleRateDTO.getId());
        if(list!=null && list.size()>0){
            return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "成功", list.get(0));
        }else{
            return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "成功", new MerchantSettleRateDTO());
        }

    }

    /**
     * 创建费率
     * @param merchantSettleRateDTO
     * @return
     */
    @PostMapping("/saveMerchantSettleRate")
    public CommonResult saveMerchantSettleRate(@RequestBody MerchantSettleRateDTO merchantSettleRateDTO) {
        if(merchantSettleRateDTO==null){
            throw new ParameterException("参数不能为空");
        }
        if(merchantSettleRateDTO.getIdentification()==null){
            throw new ParameterException("费率标识不能为空");
        }
        if(merchantSettleRateDTO.getConfigList()==null){
            throw new ParameterException("费率列表不能为空");
        }
        merchantSettleRateService.insertMerchantSettleRate(merchantSettleRateDTO);
        return new CommonResult(CommonConstants.RETURN_SUCCESS, "成功", null);
    }

    /**
     * 修改费率
     * @param dto2
     * @return
     */
    @PostMapping("/updateMerchantSettleRate")
    public CommonResult updateMerchantSettleRate(@RequestBody MerchantSettleRateDTO2 dto2) {
        if(dto2==null){
            throw new ParameterException("参数不能为空");
        }
        if(dto2.getId()==null){
            throw new ParameterException("ID不能为空");
        }
        if(dto2.getConfigList()==null){
            throw new ParameterException("费率列表不能为空");
        }
        MerchantSettleRateDTO dto=new MerchantSettleRateDTO();
        dto.setId(dto2.getId());
        dto.setIdentification(dto2.getIdentification());
        dto.setIsAgent(dto2.getIsAgent());
        dto.setIsFirst(dto2.getIsFirst());
        List<MerchantSettleRateConfigDTO> list=new ArrayList<>();
        dto2.getConfigList().forEach(v-> list.addAll(v.getList()));
        dto.setConfigList(list);
        merchantSettleRateService.updateMerchantSettleRate(dto);
        return new CommonResult(CommonConstants.RETURN_SUCCESS, "成功", null);
    }

    /**
     * 删除费率
     * @param merchantSettleRateDTO
     * @return
     */
    @PostMapping("/delMerchantSettleRate")
    public CommonResult delMerchantSettleRate(@RequestBody MerchantSettleRateDTO merchantSettleRateDTO) {
        if(merchantSettleRateDTO==null){
            throw new ParameterException("参数不能为空");
        }
        if(merchantSettleRateDTO.getId()==null){
            throw new ParameterException("ID不能为空");
        }
        if(merchantSettleRateDTO.getId()==101){
            throw new ParameterException("默认费率不能删除");
        }
        merchantSettleRateService.delMerchantSettleRate(merchantSettleRateDTO.getId());
        return new CommonResult(CommonConstants.RETURN_SUCCESS, "成功", null);
    }



}
